home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / WebControlAdapterExtender.cs440 < prev    next >
Text File  |  2007-12-22  |  23KB  |  604 lines

  1. using System;
  2. using System.Data;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Reflection;
  7. using System.Web;
  8. using System.Web.Configuration;
  9. using System.Web.Security;
  10. using System.Web.UI;
  11. using System.Web.UI.Adapters;
  12. using System.Web.UI.WebControls;
  13. using System.Web.UI.WebControls.Adapters;
  14. using System.Web.UI.WebControls.WebParts;
  15. using System.Web.UI.HtmlControls;
  16.  
  17. namespace gbweb
  18. {
  19.     public class WebControlAdapterExtender
  20.     {
  21.         private WebControl _adaptedControl = null;
  22.         public WebControl AdaptedControl
  23.         {
  24.             get
  25.             {
  26.                 System.Diagnostics.Debug.Assert(_adaptedControl != null, "CSS Friendly adapters internal error", "No control has been defined for the adapter extender");
  27.                 return _adaptedControl;
  28.             }
  29.         }
  30.  
  31.         public bool AdapterEnabled
  32.         {
  33.             get
  34.             {
  35.                 bool bReturn = true; // normally the adapters are enabled
  36.  
  37.                 //  Individual controls can use the expando property called AdapterEnabled
  38.                 //  as a way to turn the adapters off.
  39.                 //  <asp:TreeView runat="server" AdapterEnabled="false" />
  40.                 if ((AdaptedControl != null) &&
  41.                     (!String.IsNullOrEmpty(AdaptedControl.Attributes["AdapterEnabled"])) &&
  42.                     (AdaptedControl.Attributes["AdapterEnabled"].IndexOf("false", StringComparison.OrdinalIgnoreCase) == 0))
  43.                 {
  44.                     bReturn = false;
  45.                 }
  46.  
  47.                 return bReturn;
  48.             }
  49.         }
  50.  
  51.         private bool _disableAutoAccessKey = false; // used when dealing with things like read-only textboxes that should not have access keys
  52.         public bool AutoAccessKey
  53.         {
  54.             get
  55.             {
  56.                 //  Individual controls can use the expando property called AdapterEnabled
  57.                 //  as a way to turn on/off the heurisitic for automatically setting the AccessKey
  58.                 //  attribute in the rendered HTML.  The default is shown below in the initialization
  59.                 //  of the bReturn variable.
  60.                 //  <asp:TreeView runat="server" AutoAccessKey="false" />
  61.  
  62.                 bool bReturn = true; // by default, the adapter will make access keys are available
  63.                 if (_disableAutoAccessKey ||
  64.                     ((AdaptedControl != null) &&
  65.                      (!String.IsNullOrEmpty(AdaptedControl.Attributes["AutoAccessKey"])) &&
  66.                      (AdaptedControl.Attributes["AutoAccessKey"].IndexOf("false", StringComparison.OrdinalIgnoreCase) == 0)))
  67.                 {
  68.                     bReturn = false;
  69.                 }
  70.  
  71.                 return bReturn;
  72.             }
  73.         }
  74.         
  75.         public WebControlAdapterExtender(WebControl adaptedControl)
  76.         {
  77.             _adaptedControl = adaptedControl;
  78.         }
  79.  
  80.         public void RegisterScripts()
  81.         {
  82.             string folderPath = WebConfigurationManager.AppSettings.Get("CSSFriendly-JavaScript-Path");
  83.             if (String.IsNullOrEmpty(folderPath))
  84.             {
  85.                 folderPath = "~/JavaScript";
  86.             }
  87.             string filePath = folderPath.EndsWith("/") ? folderPath + "AdapterUtils.js" : folderPath + "/AdapterUtils.js";
  88.             AdaptedControl.Page.ClientScript.RegisterClientScriptInclude(GetType(), GetType().ToString(), AdaptedControl.Page.ResolveUrl(filePath));
  89.         }
  90.  
  91.         public string ResolveUrl(string url)
  92.         {
  93.             string urlToResolve = url;
  94.             int nPound = url.LastIndexOf("#");
  95.             int nSlash = url.LastIndexOf("/");
  96.             if ((nPound > -1) && (nSlash > -1) && ((nSlash + 1) == nPound))
  97.             {
  98.                 //  We have been given a somewhat strange URL.  It has a foreward slash (/) immediately followed
  99.                 //  by a pound sign (#) like this xxx/#yyy.  This sort of oddly shaped URL is what you get when
  100.                 //  you use named anchors instead of pages in the url attribute of a sitemapnode in an ASP.NET
  101.                 //  sitemap like this:
  102.                 //
  103.                 //  <siteMapNode url="#Introduction" title="Introduction"  description="Introduction" />
  104.                 //
  105.                 //  The intend of the sitemap author is clearly to create a link to a named anchor in the page
  106.                 //  that looks like these:
  107.                 //
  108.                 //  <a id="Introduction"></a>       (XHTML 1.1 Strict compliant)
  109.                 //  <a name="Introduction"></a>     (more old fashioned but quite common in many pages)
  110.                 //
  111.                 //  However, the sitemap interpretter in ASP.NET doesn't understand url values that start with
  112.                 //  a pound.  It prepends the current site's path in front of it before making it into a link
  113.                 //  (typically for a TreeView or Menu).  We'll undo that problem, however, by converting this
  114.                 //  sort of oddly shaped URL back into what was intended: a simple reference to a named anchor
  115.                 //  that is expected to be within the current page.
  116.  
  117.                 urlToResolve = url.Substring(nPound);
  118.             }
  119.             else
  120.             {
  121.                 urlToResolve = AdaptedControl.ResolveClientUrl(urlToResolve);
  122.             }
  123.  
  124.             //  And, just to be safe, we'll make sure there aren't any troublesome characters in whatever URL
  125.             //  we have decided to use at this point.
  126.             string newUrl = AdaptedControl.Page.Server.HtmlEncode(urlToResolve);
  127.  
  128.             return newUrl;
  129.         }
  130.  
  131.         public void RaiseAdaptedEvent(string eventName, EventArgs e)
  132.         {
  133.             string attr = "OnAdapted" + eventName;
  134.             if ((AdaptedControl != null) && (!String.IsNullOrEmpty(AdaptedControl.Attributes[attr])))
  135.             {
  136.                 string delegateName = AdaptedControl.Attributes[attr];
  137.                 Control methodOwner = AdaptedControl.Parent;
  138.                 MethodInfo method = methodOwner.GetType().GetMethod(delegateName);
  139.                 if (method == null)
  140.                 {
  141.                     methodOwner = AdaptedControl.Page;
  142.                     method = methodOwner.GetType().GetMethod(delegateName);
  143.                 }
  144.                 if (method != null)
  145.                 {
  146.                     object[] args = new object[2];
  147.                     args[0] = AdaptedControl;
  148.                     args[1] = e;
  149.                     method.Invoke(methodOwner, args);
  150.                 }
  151.             }
  152.         }
  153.  
  154.         public void RenderBeginTag(HtmlTextWriter writer, string cssClass)
  155.         {
  156.             string id = (AdaptedControl != null) ? AdaptedControl.ClientID : "";
  157.  
  158.             if (!String.IsNullOrEmpty(AdaptedControl.Attributes["CssSelectorClass"]))
  159.             {
  160.                 WriteBeginDiv(writer, AdaptedControl.Attributes["CssSelectorClass"], id);
  161.                 id = "";
  162.             }
  163.  
  164.             WriteBeginDiv(writer, cssClass, id);
  165.         }
  166.  
  167.         public void RenderEndTag(HtmlTextWriter writer)
  168.         {
  169.             WriteEndDiv(writer);
  170.  
  171.             if (!String.IsNullOrEmpty(AdaptedControl.Attributes["CssSelectorClass"]))
  172.             {
  173.                 WriteEndDiv(writer);
  174.             }
  175.         }
  176.  
  177.         static public void RemoveProblemChildren(Control ctrl, List<ControlRestorationInfo> stashedControls)
  178.         {
  179.             RemoveProblemTypes(ctrl.Controls, stashedControls);
  180.         }
  181.  
  182.         static public void RemoveProblemTypes(ControlCollection coll, List<ControlRestorationInfo> stashedControls)
  183.         {
  184.             foreach (Control ctrl in coll)
  185.             {
  186.                 if (typeof(RequiredFieldValidator).IsAssignableFrom(ctrl.GetType()) ||
  187.                     typeof(CompareValidator).IsAssignableFrom(ctrl.GetType()) ||
  188.                     typeof(RegularExpressionValidator).IsAssignableFrom(ctrl.GetType()) ||
  189.                     typeof(ValidationSummary).IsAssignableFrom(ctrl.GetType()))
  190.                 {
  191.                     ControlRestorationInfo cri = new ControlRestorationInfo(ctrl, coll);
  192.                     stashedControls.Add(cri);
  193.                     coll.Remove(ctrl);
  194.                     continue;
  195.                 }
  196.  
  197.                 if (ctrl.HasControls())
  198.                 {
  199.                     RemoveProblemTypes(ctrl.Controls, stashedControls);
  200.                 }
  201.             }
  202.         }
  203.  
  204.         static public void RestoreProblemChildren(List<ControlRestorationInfo> stashedControls)
  205.         {
  206.             foreach (ControlRestorationInfo cri in stashedControls)
  207.             {
  208.                 cri.Restore();
  209.             }
  210.         }
  211.  
  212.         public string MakeChildId(string postfix)
  213.         {
  214.             return AdaptedControl.ClientID + "_" + postfix;
  215.         }
  216.  
  217.         static public string MakeNameFromId(string id)
  218.         {
  219.             string name = "";
  220.             for (int i=0; i<id.Length; i++)
  221.             {
  222.                 char thisChar = id[i];
  223.                 char prevChar = ((i - 1) > -1) ? id[i - 1] : ' ';
  224.                 char nextChar = ((i + 1) < id.Length) ? id[i + 1] : ' ';
  225.                 if (thisChar == '_')
  226.                 {
  227.                     if (prevChar == '_')
  228.                     {
  229.                         name += "_";
  230.                     }
  231.                     else if (nextChar == '_')
  232.                     {
  233.                         name += "$_";
  234.                     }
  235.                     else
  236.                     {
  237.                         name += "$";
  238.                     }
  239.                 }
  240.                 else
  241.                 {
  242.                     name += thisChar;
  243.                 }
  244.             }
  245.             return name;
  246.         }
  247.  
  248.         static public string MakeIdWithButtonType(string id, ButtonType type)
  249.         {
  250.             string idWithType = id;
  251.             switch (type)
  252.             {
  253.                 case ButtonType.Button:
  254.                     idWithType += "Button";
  255.                     break;
  256.                 case ButtonType.Image:
  257.                     idWithType += "ImageButton";
  258.                     break;
  259.                 case ButtonType.Link:
  260.                     idWithType += "LinkButton";
  261.                     break;
  262.             }
  263.             return idWithType;
  264.         }
  265.  
  266.         public string MakeChildName(string postfix)
  267.         {
  268.             return MakeNameFromId(MakeChildId(postfix));
  269.         }
  270.  
  271.         static public void WriteBeginDiv(HtmlTextWriter writer, string className, string id)
  272.         {
  273.             writer.WriteLine();
  274.             writer.WriteBeginTag("div");
  275.             if (!String.IsNullOrEmpty(className))
  276.             {
  277.                 writer.WriteAttribute("class", className);
  278.             }
  279.             if (!String.IsNullOrEmpty(id))
  280.             {
  281.                 writer.WriteAttribute("id", id);
  282.             }
  283.             writer.Write(HtmlTextWriter.TagRightChar);
  284.             writer.Indent++;
  285.         }
  286.  
  287.         static public void WriteEndDiv(HtmlTextWriter writer)
  288.         {
  289.             writer.Indent--;
  290.             writer.WriteLine();
  291.             writer.WriteEndTag("div");
  292.         }
  293.  
  294.         static public void WriteSpan(HtmlTextWriter writer, string className, string content)
  295.         {
  296.             if (!String.IsNullOrEmpty(content))
  297.             {
  298.                 writer.WriteLine();
  299.                 writer.WriteBeginTag("span");
  300.                 if (!String.IsNullOrEmpty(className))
  301.                 {
  302.                     writer.WriteAttribute("class", className);
  303.                 }
  304.                 writer.Write(HtmlTextWriter.TagRightChar);
  305.                 writer.Write(content);
  306.                 writer.WriteEndTag("span");
  307.             }
  308.         }
  309.  
  310.         static public void WriteImage(HtmlTextWriter writer, string url, string alt)
  311.         {
  312.             if (!String.IsNullOrEmpty(url))
  313.             {
  314.                 writer.WriteLine();
  315.                 writer.WriteBeginTag("img");
  316.                 writer.WriteAttribute("src", url);
  317.                 writer.WriteAttribute("alt", alt);
  318.                 writer.Write(HtmlTextWriter.SelfClosingTagEnd);
  319.             }
  320.         }
  321.  
  322.         static public void WriteLink(HtmlTextWriter writer, string className, string url, string title, string content)
  323.         {
  324.             if ((!String.IsNullOrEmpty(url)) && (!String.IsNullOrEmpty(content)))
  325.             {
  326.                 writer.WriteLine();
  327.                 writer.WriteBeginTag("a");
  328.                 if (!String.IsNullOrEmpty(className))
  329.                 {
  330.                     writer.WriteAttribute("class", className);
  331.                 }
  332.                 writer.WriteAttribute("href", url);
  333.                 writer.WriteAttribute("title", title);
  334.                 writer.Write(HtmlTextWriter.TagRightChar);
  335.                 writer.Write(content);
  336.                 writer.WriteEndTag("a");
  337.             }
  338.         }
  339.  
  340.         //  Can't be static because it uses MakeChildId
  341.         public void WriteLabel(HtmlTextWriter writer, string className, string text, string forId)
  342.         {
  343.             if (!String.IsNullOrEmpty(text))
  344.             {
  345.                 writer.WriteLine();
  346.                 writer.WriteBeginTag("label");
  347.                 writer.WriteAttribute("for", MakeChildId(forId));
  348.                 if (!String.IsNullOrEmpty(className))
  349.                 {
  350.                     writer.WriteAttribute("class", className);
  351.                 }
  352.                 writer.Write(HtmlTextWriter.TagRightChar);
  353.  
  354.                 if (AutoAccessKey)
  355.                 {
  356.                     writer.WriteBeginTag("em");
  357.                     writer.Write(HtmlTextWriter.TagRightChar);
  358.                     writer.Write(text[0].ToString());
  359.                     writer.WriteEndTag("em");
  360.                     if (!String.IsNullOrEmpty(text))
  361.                     {
  362.                         writer.Write(text.Substring(1));
  363.                     }
  364.                 }
  365.                 else
  366.                 {
  367.                     writer.Write(text);
  368.                 }
  369.  
  370.                 writer.WriteEndTag("label");
  371.             }
  372.         }
  373.  
  374.         //  Can't be static because it uses MakeChildId
  375.         public void WriteTextBox(HtmlTextWriter writer, bool isPassword, string labelClassName, string labelText, string inputClassName, string id, string value)
  376.         {
  377.             WriteLabel(writer, labelClassName, labelText, id);
  378.  
  379.             writer.WriteLine();
  380.             writer.WriteBeginTag("input");
  381.             writer.WriteAttribute("type", isPassword ? "password" : "text");
  382.             if (!String.IsNullOrEmpty(inputClassName))
  383.             {
  384.                 writer.WriteAttribute("class", inputClassName);
  385.             }
  386.             writer.WriteAttribute("id", MakeChildId(id));
  387.             writer.WriteAttribute("name", MakeChildName(id));
  388.             writer.WriteAttribute("value", value);
  389.             if (AutoAccessKey && (!String.IsNullOrEmpty(labelText)))
  390.             {
  391.                 writer.WriteAttribute("accesskey", labelText[0].ToString().ToLower());
  392.             }
  393.             
  394.             writer.Write(HtmlTextWriter.SelfClosingTagEnd);
  395.         }
  396.  
  397.         //  Can't be static because it uses MakeChildId
  398.         public void WriteReadOnlyTextBox(HtmlTextWriter writer,  string labelClassName, string labelText, string inputClassName, string value)
  399.         {
  400.             bool oldDisableAutoAccessKey = _disableAutoAccessKey;
  401.             _disableAutoAccessKey = true;
  402.  
  403.             WriteLabel(writer, labelClassName, labelText, "");
  404.  
  405.             writer.WriteLine();
  406.             writer.WriteBeginTag("input");
  407.             writer.WriteAttribute("readonly", "readonly");
  408.             if (!String.IsNullOrEmpty(inputClassName))
  409.             {
  410.                 writer.WriteAttribute("class", inputClassName);
  411.             }
  412.             writer.WriteAttribute("value", value);
  413.             writer.Write(HtmlTextWriter.SelfClosingTagEnd);
  414.  
  415.             _disableAutoAccessKey = oldDisableAutoAccessKey;
  416.         }
  417.  
  418.         //  Can't be static because it uses MakeChildId
  419.         public void WriteCheckBox(HtmlTextWriter writer, string labelClassName, string labelText, string inputClassName, string id, bool isChecked)
  420.         {
  421.             writer.WriteLine();
  422.             writer.WriteBeginTag("input");
  423.             writer.WriteAttribute("type", "checkbox");
  424.             if (!String.IsNullOrEmpty(inputClassName))
  425.             {
  426.                 writer.WriteAttribute("class", inputClassName);
  427.             }
  428.             writer.WriteAttribute("id", MakeChildId(id));
  429.             writer.WriteAttribute("name", MakeChildName(id));
  430.             if (isChecked)
  431.             {
  432.                 writer.WriteAttribute("checked", "checked");
  433.             }
  434.             if (AutoAccessKey && (!String.IsNullOrEmpty(labelText)))
  435.             {
  436.                 writer.WriteAttribute("accesskey", labelText[0].ToString());
  437.             }
  438.             writer.Write(HtmlTextWriter.SelfClosingTagEnd);
  439.  
  440.             WriteLabel(writer, labelClassName, labelText, id);
  441.         }
  442.  
  443.         //  Can't be static because it uses MakeChildId
  444.         public void WriteSubmit(HtmlTextWriter writer, ButtonType buttonType, string className, string id, string imageUrl, string javascript, string text)
  445.         {
  446.             writer.WriteLine();
  447.  
  448.             string idWithType = id;
  449.  
  450.             switch (buttonType)
  451.             {
  452.                 case ButtonType.Button:
  453.                     writer.WriteBeginTag("input");
  454.                     writer.WriteAttribute("type", "submit");
  455.                     writer.WriteAttribute("value", text);
  456.                     idWithType += "Button";
  457.                     break;
  458.                 case ButtonType.Image:
  459.                     writer.WriteBeginTag("input");
  460.                     writer.WriteAttribute("type", "image");
  461.                     writer.WriteAttribute("src", imageUrl);
  462.                     idWithType += "ImageButton";
  463.                     break;
  464.                 case ButtonType.Link:
  465.                     writer.WriteBeginTag("a");
  466.                     idWithType += "LinkButton";
  467.                     break;
  468.             }
  469.  
  470.             if (!String.IsNullOrEmpty(className))
  471.             {
  472.                 writer.WriteAttribute("class", className);
  473.             }
  474.             writer.WriteAttribute("id", MakeChildId(idWithType));
  475.             writer.WriteAttribute("name", MakeChildName(idWithType));
  476.  
  477.             if (!String.IsNullOrEmpty(javascript))
  478.             {
  479.                 string pureJS = javascript;
  480.                 if (pureJS.StartsWith("javascript:"))
  481.                 {
  482.                     pureJS = pureJS.Substring("javascript:".Length);
  483.                 }
  484.                 switch (buttonType)
  485.                 {
  486.                     case ButtonType.Button:
  487.                         writer.WriteAttribute("onclick", pureJS);
  488.                         break;
  489.                     case ButtonType.Image:
  490.                         writer.WriteAttribute("onclick", pureJS);
  491.                         break;
  492.                     case ButtonType.Link:
  493.                         writer.WriteAttribute("href", javascript);
  494.                         break;
  495.                 }
  496.             }
  497.  
  498.             if (buttonType == ButtonType.Link)
  499.             {
  500.                 writer.Write(HtmlTextWriter.TagRightChar);
  501.                 writer.Write(text);
  502.                 writer.WriteEndTag("a");
  503.             }
  504.             else
  505.             {
  506.                 writer.Write(HtmlTextWriter.SelfClosingTagEnd);
  507.             }
  508.         }
  509.  
  510.         static public void WriteRequiredFieldValidator(HtmlTextWriter writer, RequiredFieldValidator rfv, string className, string controlToValidate, string msg)
  511.         {
  512.             if (rfv != null)
  513.             {
  514.                 rfv.CssClass = className;
  515.                 rfv.ControlToValidate = controlToValidate;
  516.                 rfv.ErrorMessage = msg;
  517.                 rfv.RenderControl(writer);
  518.             }
  519.         }
  520.  
  521.         static public void WriteRegularExpressionValidator(HtmlTextWriter writer, RegularExpressionValidator rev, string className, string controlToValidate, string msg, string expression)
  522.         {
  523.             if (rev != null)
  524.             {
  525.                 rev.CssClass = className;
  526.                 rev.ControlToValidate = controlToValidate;
  527.                 rev.ErrorMessage = msg;
  528.                 rev.ValidationExpression = expression;
  529.                 rev.RenderControl(writer);
  530.             }
  531.         }
  532.  
  533.         static public void WriteCompareValidator(HtmlTextWriter writer, CompareValidator cv, string className, string controlToValidate, string msg, string controlToCompare)
  534.         {
  535.             if (cv != null)
  536.             {
  537.                 cv.CssClass = className;
  538.                 cv.ControlToValidate = controlToValidate;
  539.                 cv.ErrorMessage = msg;
  540.                 cv.ControlToCompare = controlToCompare;
  541.                 cv.RenderControl(writer);
  542.             }
  543.         }
  544.  
  545.         static public void WriteTargetAttribute(HtmlTextWriter writer, string targetValue)
  546.         {
  547.             if ((writer != null) && (!String.IsNullOrEmpty(targetValue)))
  548.             {
  549.                 //  If the targetValue is _blank then we have an opportunity to use attributes other than "target"
  550.                 //  which allows us to be compliant at the XHTML 1.1 Strict level. Specifically, we can use a combination
  551.                 //  of "onclick" and "onkeypress" to achieve what we want to achieve when we used to render
  552.                 //  target='blank'.
  553.                 //
  554.                 //  If the targetValue is other than _blank then we fall back to using the "target" attribute.
  555.                 //  This is a heuristic that can be refined over time.
  556.                 if (targetValue.Equals("_blank", StringComparison.OrdinalIgnoreCase))
  557.                 {
  558.                     string js = "window.open(this.href, '_blank', ''); return false;";
  559.                     writer.WriteAttribute("onclick", js);
  560.                     writer.WriteAttribute("onkeypress", js);
  561.                 }
  562.                 else
  563.                 {
  564.                     writer.WriteAttribute("target", targetValue);
  565.                 }
  566.             }
  567.         }
  568.     }
  569.  
  570.     public class ControlRestorationInfo
  571.     {
  572.         private Control _ctrl = null;
  573.         public Control Control
  574.         {
  575.             get { return _ctrl; }
  576.         }
  577.  
  578.         private ControlCollection _coll = null;
  579.         public ControlCollection Collection
  580.         {
  581.             get { return _coll; }
  582.         }
  583.  
  584.         public bool IsValid
  585.         {
  586.             get { return (Control != null) && (Collection != null); }
  587.         }
  588.  
  589.         public ControlRestorationInfo(Control ctrl, ControlCollection coll)
  590.         {
  591.             _ctrl = ctrl;
  592.             _coll = coll;
  593.         }
  594.  
  595.         public void Restore()
  596.         {
  597.             if (IsValid)
  598.             {
  599.                 _coll.Add(_ctrl);
  600.             }
  601.         }
  602.     }
  603. }
  604.